<?php

namespace app\common\model;

use think\Model;
use think\model\relation\BelongsTo;

/**
 * em_order
 * @property integer $id (主键)
 * @property string $order_sn 订单号
 * @property integer $dept_id 始发站
 * @property string $end_dept_id 终点站
 * @property string $address 始发地址
 * @property string $end_address 终点地址
 * @property string $nickname 建单人
 * @property integer $user_id 建单人
 * @property string $commission 提成
 * @property string $remark 业绩备注信息
 * @property string $shipper_mobile 发送人电话
 * @property string $shipper_name 发送人姓名
 * @property integer $shipper_type 发送人类型
 * @property string $accept_mobile 接受人电话
 * @property string $accept_name 接受人姓名
 * @property integer $pay_type 支付方式 到付终点结算， 现付始发站结算
 * @property string $total_price 应收总费
 * @property string $car_brand 车辆品牌
 * @property string $car_shelf_num 大架号
 * @property integer $car_worth 车辆价值(万)
 * @property string $car_color 车辆颜色
 * @property string $car_number 车牌号
 * @property integer $insure_type 保险类型
 * @property string $insure_price 保价费
 * @property string $contract 合同照片
 * @property integer $status 订单状态 4订单完结 6车辆退回
 * @property string $create_time
 * @property string $update_time
 * @property string $delete_time
 * @property string $eta 预计到达
 */
class Order extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';
    
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'em_order';
    
    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';
    
    const PAY_TYPE = [
        ['id' => 1, 'title' => '到付支付'],
        ['id' => 2, 'title' => '现付支付'],
//        ['id' => 3, 'title' => '欠付支付']
    ];
    
    const SHIPPER_TYPE = [
        ['id' => 1, 'title' => '个人类型'],
        ['id' => 2, 'title' => '公司类型']
    ];
    
    const STATUS = [
        0 => '签合同',
        1 => '运输中',
        // 2 => '待发中',
        // 3 => '已发出',
        4 => '已完结',
        5 => '已退回',
        8 => '已作废'
    ];

    const ORDER_TRIP_STATUS = [
        0 => '验车中',
        1 => '提回待发',
        2 => '配板发车',
        3 => '已发车',
        4 => '已完结',
    ];
    
    const CONTRACT_STATUS = [
        0 => '未签署',
        1 => '已签署',
    ];
    
    const COLLECTION_CAR_TYPE = [
        ['id' => 1, 'title' => '派送'],
        ['id' => 2, 'title' => '自提']
    ];
    
    /**
     * 保险类型
     */
    public function insure(): BelongsTo
    {
        return $this->belongsTo('Insure', 'insure_type')->bind(['insure_text' => 'title']);
    }
    
    
    public function getStatusTextAttr($value, $data): string
    {
        return self::STATUS[$data['status']];
    }
    
    public function getPayTypeTextAttr($value, $data): string
    {
        $index = arr2Search($data['pay_type'], self::SHIPPER_TYPE, 'id');
        return self::PAY_TYPE[$index]['title'];
    }
    
    public function getShipperTypeTextAttr($value, $data): string
    {
        $index = arr2Search($data['shipper_type'], self::SHIPPER_TYPE, 'id');
        return self::SHIPPER_TYPE[$index]['title'];
    }
    
    public function getContractTextAttr($value,$data){
        return self::CONTRACT_STATUS[$data['contract_status']];
    }
    
    public function getCollectionCarTypeTextAttr($value,$data){
        return self::COLLECTION_CAR_TYPE[$data['collection_car_type']]['title'];
    }
    
    
    public function dept(): BelongsTo
    {
        return $this->belongsTo('Dept', 'dept_id')->bind(['dept_name' => 'name']);
    }
    
    public function endDept(): BelongsTo
    {
        return $this->belongsTo('Dept', 'end_dept_id')->bind(['end_dept_name' => 'name']);
    }

    /**
     * 关联订单操作
     * @return \think\model\relation\HasMany
     */
    public function logisticsLog()
    {
        return $this->hasMany(OrderLogisticsLog::class, 'order_id', 'id');
    }

    /**
     * 关联订单验收
     * @return \think\model\relation\HasMany
     */
    public function trips()
    {
        return $this->hasMany(OrderTrip::class, 'order_id', 'id');
    }

    /**
     * 关联配板发车详情
     * @return \think\model\relation\HasMany
     */
    public function boardItem()
    {
        return $this->hasMany(OrderBoardItem::class, 'order_id', 'id');
    }
}
